Option Explicit

' -----------------------------------------------------------------------------
'  File:       /src/vbs/Generic URL Context AppScript Runner.vbs
'  Date:       5:32 PM 7/11/2013
'  Author:     Paul Thompson (paul_m_thompson@yahoo.com)

'  Access:     $Access$
'  ScriptName: $ScriptName$
'  File:       $File$
'  Author:     $Author: mnemotronic $
'  Date:       $Date: 2013-11-28 00:40:55 +0000 (Thu, 28 Nov 2013) $
'  URL:        $URL: http://serena-teamtrack-mashups.googlecode.com/svn/vbs/util/Generic%20URL%20Context%20AppScript%20Runner.vbs $
'  Id:         $Id: Generic URL Context AppScript Runner.vbs 168 2013-11-28 00:40:55Z mnemotronic $
'  Revision:   $Revision: 168 $


' -----------------------------------------------------------------------------

' Call this script from a Windows Scheduled task to run a URL context AppScript.

'  Settings for Windows Scheduled Tasks:
'     Run:
'        ***path to cscript.exe***    ***Name of this script***.vbs      ***AppScript params***      /DryRun:***true or false***      /Log:***true or false***
'     Start in:
'        "***location of this script***"
'     Run As:
'        NT AUTHORITY\SYSTEM

' -----------------------------------------------------------------------------

' Replace any strings that look like "***something***"

' These mimic the SBM AppScript Shell.Variables but by necessity for this context are hard-coded.
' Obviously it's bad security practice to hard-code user IDs and passwords into a file, but this
'  file is on the server and if someone is reading it, they have access to the server.
CONST SHELL_URLProtocol = "http"
CONST SHELL_URLServer = "***my.SERVER.com***"
CONST SHELL_URLPort = 80
CONST SHELL_URLPath = "/tmtrack/tmtrack.dll"
CONST APPSCRIPT = "***MyAppScriptName***"

' !!! Enable one of the following 2 CONST lines !!!
' two options for authorization:  Username and password in the clear
' CONST SCRIPTAUTH = "&ttAuthUID=***user name***&ttAuthPWD=***user password***"
' or "username:password" encoded with base64: username:password==dXNlcm5hbWU6cGFzc3dvcmQ=
CONST SCRIPTAUTH = "&ttAuthInfo=dXNlcm5hbWU6cGFzc3dvcmQ="



Dim WshShell , obj_Named_Args , obj_Arg , xmlHttpObj
Dim  str_Url , nStatus

' Params from the Scheduled Task run line that are used locally.
Dim bDryRun , bLog , bEcho

' Params from the Scheduled Task run line passed to AppScript in URL.
Dim str_AppScript_Params


bDryRun = false : bLog = false : bEcho = false

Set WshShell = WScript.CreateObject("WScript.Shell")
Set obj_Named_Args = WScript.Arguments.Named


' WScript.Arguments.Named is a key:value collection specified on command line like this "/key:value"

'     /DryRun:true         Will process params, log stuff, but not actually call the AppScript
'     /Log:true            Enable logging
'     /Echo:true           Also echo logging info to console

'	Param          TypeName ( WScript.Arguments.Named.Item("key") )
'	----------     --------------------------------------------------
' 	/key	         Empty
'	/key:				String; Len=0
'	/key:value		String; Len>0



'	Get cmd line args.
For Each obj_Arg In obj_Named_Args
   '  Params for CBool must be "true", "false" or numeric (0 evaluates to "false", all other values are "true")
   If LCase(obj_Arg) = "dryrun" Then
      bDryRun = CBool(obj_Named_Args.Item(obj_Arg))
   ElseIf LCase(obj_Arg) = "log" Then
      bLog = CBool(obj_Named_Args.Item(obj_Arg))
   ElseIf LCase(obj_Arg) = "echo" Then
      bEcho = CBool(obj_Named_Args.Item(obj_Arg))
   Else
      ' Processing for other params to be consumed locally or passed to URL context AppScript
      str_AppScript_Params = str_AppScript_Params & "&" & obj_Arg & "=" & obj_Named_Args.Item(obj_Arg)
   End If
Next

' Don't need args object anymore.
Set obj_Named_Args = Nothing


Call EvtLogger ("AppScript DryRun mode=" & bDryRun & ";  AppScript params='" & str_AppScript_Params & "'")

' -----------------------------------------------------------------------------

' Original idea from: http://www.paulsadowski.com/wsh/xmlhttp.htm
' This is how we run the URL context AppScript
Set xmlHttpObj = CreateObject("Microsoft.XmlHttp")
str_Url =   SHELL_URLProtocol & "://" & SHELL_URLServer & ":" & SHELL_URLPort & _
            SHELL_URLPath & "?ScriptPage&scriptName=" & APPSCRIPT & SCRIPTAUTH & _
            str_AppScript_Params

If bDryRun Then
   Call EvtLogger ("DryRun mode:: GET '" & str_Url & "'")
   nStatus = 0
Else
   Call EvtLogger ("GET '" & str_Url & "'")
   xmlHttpObj.open "GET", str_Url , FALSE

   Call EvtLogger ("send '' ")
   xmlHttpObj.send ""

   'WScript.Echo xmlHttpObj.responseText
   nStatus = xmlHttpObj.Status
   Call EvtLogger ("Status=" & nStatus & "; responseText=" & xmlHttpObj.responseText)

   ' OPTIONAL: logging to a text file.
   'set fs = createobject("Scripting.FileSystemObject")
   'set txtStr = fs.openTextFile("c:\script_out.html",2,1)
   'txtStr.writeline(xmlHttpObj.responseText)
   'txtStr.close

End If

Set xmlHttpObj = Nothing
' !!! No more logging after the next line !!!
Set WshShell = Nothing

' Return status for the Scheduled Event log.
if nStatus=200 Then WScript.Quit 0 Else WScript.Quit 1

' -----------------------------------------------------------------------------

Sub EvtLogger(str)
   If bLog Then WshShell.LogEvent 4, "Scheduled Event Script: " & WScript.ScriptName & "; " & str
   If bEcho Then WScript.Echo str
End Sub

